package com.github.kezhenxu94.playground.java.network;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SpamChecker {
	
	private static final Logger	LOG			= Logger.getLogger(SpamChecker.class.getName());
	private static final String	BLACK_HOLE	= "sbl.spamhaus.org";
	
	public boolean isSpam(String address) {
		try {
			InetAddress inetAddress = InetAddress.getByName(address);
			byte[] dottedQuad = inetAddress.getAddress();
			StringBuilder builder = new StringBuilder(BLACK_HOLE);
			for (byte b : dottedQuad) {
				builder.insert(0, "").insert(0, b > 0 ? b : (256 + b));
			}
			LOG.log(Level.INFO, "Sending request to " + builder.toString());
			InetAddress.getByName(builder.toString());
			return true;
		} catch (UnknownHostException e) {
			return false;
		}
	}
}
